from captcha.fields import CaptchaField, CaptchaTextInput
from django import forms
from .models import CustomUser
from main.models import Article


class UserPassWordChangeForm(forms.Form):
    old_password = forms.CharField(label="原始密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2





# class UserLoginForm(forms.Form):
#     username = forms.CharField(label="用户名", widget=forms.TextInput(attrs={
#         "class": "form-control",
#         "placeholder": "请输入用户名"
#     }))
#     password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
#         # "class": "form-control",
#         "placeholder": "请输入密码"
#     }))



class UserRegisterForm(forms.ModelForm):
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))
    class Meta:
        model = CustomUser
        fields = ['username']
        widgets = {
            'username': forms.TextInput(attrs={
                "class": "form-control",
                "placeholder": "请输入用户名"
            }),

        }

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2


class UserLoginForm(forms.Form):
    username = forms.CharField(label="用户名", widget=forms.TextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入用户名"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    captcha = CaptchaField(label='验证码',widget= CaptchaTextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入验证码"
    }))


class UserInfoChangeForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ['username', 'head']
        widgets = {
            'username': forms.TextInput(attrs={
                "class": "form-control",
                "placeholder": "请输入用户名"
            }),
            'head': forms.FileInput(attrs={})

        },
        labels = {
            'head': "头像"
        }

    def clean_head(self):
        head = self.cleaned_data.get('head')
        print(head, "0000")
        return head

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ['title','content','tags','categories']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'content': forms.Textarea(attrs={'class': 'form-control'}),
            'tags': forms.SelectMultiple(attrs={'class': 'form-control'}),
            'categories': forms.SelectMultiple(attrs={'class': 'form-control'}),
        }